const Controller = require('egg').Controller;
const ErrorUtil = require('../../class/error');
class UserController extends Controller {
  async list() {
    const ctx = this.ctx;
    const app = this.app;
    const query = ctx.query;
    let params = {
      pageSize: +query.pageSize || app.config.pageBean.pageSize,
      pageNo: +query.pageNo || 1,
      orderBy: query.orderBy || 'create_time',
      order: query.order || 'DESC',
      keyword: query.keyword || '',
      userType: query.userType
    }
    let where = {
      $or: [
        {
          user_id: {
            $like: '%' + params.keyword + '%'
          }
        },
        {
          real_name: {
            $like: '%' + params.keyword + '%'
          }
        }
      ],
    };
    if (params.userType)
      where.user_type = params.userType;
    const data = await this.service.user.list(
      params.pageNo,
      params.pageSize,
      params.orderBy,
      params.order,
      where
    );
    params.totalSize = data.count;
    params.totalPage = data.count == 0 ? 1 : Math.ceil(data.count / params.pageSize);
    ctx.locals = {
      params: params,
      list: data.rows,
    }

    await ctx.render('admin/user/list.html');
  }

  /**
   * @description 修改用户权限
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof UserController
   */
  async editUserTypeJson(){
    const ctx = this.ctx;
    const body = ctx.request.body;
    try{
      await this.service.user.update(+body.userId, {
        user_type: +body.userType
      })
      ctx.body = ErrorUtil.SUCCESS;
    }catch(e){
      ctx.body = ErrorUtil.UNKOWN;
      throw e;
    }

  }
}

module.exports = UserController;